package analyzeLogFiles;

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;

import java.io.*;
import database.*;
import java.util.ArrayList;
import analyzeLogFiles.AnalyzeLogLine;

/**
 * Testing class for AnalyzeLogLing class which handles log line parsing
 * 		and sends data to database.
 * @author Gabrielle DuBois
 */
public class AnalyzeLogTests {
	private AnalyzeLogLine all;
	private String ip1;
	private String ip2;

	/**
	 * Initializes new AnalyzeLogLine along with test IP Strings.
	 */
	@Before
	public void setUp() {
		all = new AnalyzeLogLine();
		ip1 = "64.233.161.99";
		ip2 = "192.168.001.143";
	}

	/**
	 * Testing 4 example log lines. A new connection, a random,
	 * 		a visualization usage, and a visualization name
	 */
	@Test
	public void test01() {
		boolean truth;
		String serverName = "Zola";
		String logLine = "1/10/09 5:59 AM - INFO: Accepted connection 36 from 84.557.39.36";

		truth = all.acceptLogLine(serverName, logLine);
		assertTrue(truth);

		logLine = "1/10/07 5:59 AM - FINE: sendtoClient - 100 Client and server connected with user login no_username";
		truth = all.acceptLogLine(serverName, logLine);
		assertFalse(truth);

		logLine = "1/10/07 5:59 AM - FINE: http://jhave.org/jhave/html_root/uid/36.sho";
		truth = all.acceptLogLine(serverName, logLine);
		assertTrue(truth);

		logLine = "1/10/07 5:59 AM - FINE: sendtoClient - 400 bintreemyles gaigs";
		truth = all.acceptLogLine(serverName, logLine);
		assertTrue(truth);
	}

	/**
	 * Verifies that AnalyzeLogLine.ipLookup() can take and IP address and
	 * an String array with three values containing country, region, city in
	 * that order
	 */
	@Test
	public void testIPLookup() {
		String [] ary = all.ipLookup(ip1);

		assertTrue( ary[0].equals("United States") );
		assertTrue( ary[1].equals("California") );
		assertTrue( ary[2].equals("Mountain View") );
	}

	/**
	 * Running through first log file in tarball
	 */
	@Test
	public void testTarBallAction1() {
		boolean truth;
		String logLine;
		String serverName = "Albatross";
		String preFileName = "tests/analyzeLogFiles/tarballs/logs/";
		String postFileName1 = "jhave-0.log.1";


		try{
			FileReader fr = new FileReader(preFileName + postFileName1);
			BufferedReader br = new BufferedReader(fr);

			while( (logLine = br.readLine()) != null ) {
				all.acceptLogLine(serverName, logLine );
			}

			assertTrue(true);
		} catch(Exception e){
			//assertFalse(true);
			System.out.println("Error in reading tarball1: "+e);
		}
	}//end

	/**
	 * Running through second log file in tarball
	 */
	@Test
	public void testTarBallAction2() {
		boolean truth;
		String logLine;
		String serverName = "Albatross";
		String preFileName = "tests/analyzeLogFiles/tarballs/logs/";
		String postFileName1 = "jhave-0.log.2";


		try{
			FileReader fr = new FileReader(preFileName + postFileName1);
			BufferedReader br = new BufferedReader(fr);

			while( (logLine = br.readLine()) != null ) {
				all.acceptLogLine(serverName, logLine );
			}

			assertTrue(true);
		} catch(Exception e){
			//assertFalse(true);
			System.out.println("Error in reading tarball2: "+e);
		}
	}//end

	/**
	 * Running through third log file in tarball
	 */
	@Test
	public void testTarBallAction3() {
		boolean truth;
		String logLine;
		String serverName = "Albatross";
		String preFileName = "tests/analyzeLogFiles/tarballs/logs/";
		String postFileName1 = "jhave-0.log.3";


		try{
			FileReader fr = new FileReader(preFileName + postFileName1);
			BufferedReader br = new BufferedReader(fr);

			while( (logLine = br.readLine()) != null ) {
				all.acceptLogLine(serverName, logLine );
			}

			assertTrue(true);
		} catch(Exception e){
			//assertFalse(true);
			System.out.println("Error in reading tarball3: "+e);
		}
	}//end

	/**
	 * Running through fourth log file in tarball
	 */
	@Test
	public void testTarBallAction4() {
		boolean truth;
		String logLine;
		String serverName = "Albatross";
		String preFileName = "tests/analyzeLogFiles/tarballs/logs/";
		String postFileName1 = "jhave-0.log.4";


		try{
			FileReader fr = new FileReader(preFileName + postFileName1);
			BufferedReader br = new BufferedReader(fr);

			while( (logLine = br.readLine()) != null ) {
				all.acceptLogLine(serverName, logLine );
			}

			assertTrue(true);
		} catch(Exception e){
			//assertFalse(true);
			System.out.println("Error in reading tarball4: "+e);
		}
	}//end

}//AnalyzeLogTests
